from django.http import JsonResponse
from django.utils.deprecation import MiddlewareMixin
import time
import json
from constr_budgets.models import People


class MD1(MiddlewareMixin):
    def process_request(self, req):
        if req.path != "/api/login":
            print(1111)

            try:
                token = req.META.get("HTTP_AUTHORIZATION").split(" ")[1]
                if token=="miniproject":
                    return
                data = list(People.objects.filter(token=token).values())
                if data is None:
                    return JsonResponse({"code": 400, "msg": "token无效"})
                elif float(data[0]["end_token"]) < time.time():
                    return JsonResponse({"code": 401, "msg": "token过期"})
            except AttributeError:
                return JsonResponse({"code": 400, "msg": "token无效"})
            except IndexError:
                return JsonResponse({"code": 400, "msg": "token无效"})

